package com.bole.mq;

import com.alibaba.fastjson.JSONObject;
import com.bole.service.EsService;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RocketMQMessageListener(topic = "BOLE",consumerGroup = "consumerGroup1", messageModel = MessageModel.BROADCASTING)
public class EsComsumer implements RocketMQListener<String> {
    @Autowired
    private EsService esService;

    @Override
    public void onMessage(String s) {
        System.out.println("进入es的消费者");
        try {
            List<Long> longs = JSONObject.parseArray(s, Long.class);
            // List转成数组
            Long[] ids = longs.toArray(new Long[0]);
            esService.insertElasticsearchIndex(ids);
            System.out.println("es消费者消费成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("es消费失败错误");
        }
    }
}
